找传奇、传世资源到传世资源站!

C# TCP 文件传输 源码下载

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

利用tcp socket 通讯实现
接收端截图:C# TCP 文件传输 源码下载 C#网络编程-第1张
发送端截图:C# TCP 文件传输 源码下载 C#网络编程-第2张  发送端代码:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.IO;using System.Threading;namespace Sender{ public partial class Form1 : Form { private Thread worker;//这是一个工作线程 private string hostname; private int port; private string filename; public Form1() { InitializeComponent(); } private void btnStart_Click(object sender, EventArgs e) { if (txtIp.Text.Trim() == "") { MessageBox.Show("ip地址不能为空!"); txtIp.Focus(); return; } if (int.Parse(txtPort.Text.Trim()) < 1 || int.Parse(txtPort.Text.Trim()) > 65535) { MessageBox.Show("端口范围无效!"); txtPort.Focus(); return; } hostname = txtIp.Text; port = int.Parse(txtPort.Text); OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = "c:/"; ofd.Filter = "所有文件|*.*"; ofd.FilterIndex = 1; if (ofd.ShowDialog(this) == DialogResult.OK) filename = ofd.FileName; if (!File.Exists(filename)) { MessageBox.Show("文件不存在!"); return; } btnStart.Enabled = false; btnCancel.Enabled = true; worker = new Thread(new ThreadStart(Start)); worker.IsBackground = true; worker.Start(); } private void Start() { try { this.Invoke((EventHandler)(delegate { tlblState.Text = "状态:正在连接..."; progressBar1.Minimum = 0; })); TcpClient tcpClient = new TcpClient(); tcpClient.NoDelay = true; tcpClient.SendTimeout = 30000; try { tcpClient.Connect(hostname, port); } catch (Exception ex1) { this.Invoke((EventHandler)(delegate { tlblState.Text = "状态:连接出错..."; btnCancel.Enabled = false; btnStart.Enabled = true; })); worker = null; return; } BinaryWriter writer = new BinaryWriter(tcpClient.GetStream()); try { this.Invoke((EventHandler)(delegate { tlblState.Text = "状态:正在发送文件信息..."; })); FileInfo fi = new FileInfo(filename); writer.Write(fi.Name); writer.Write(fi.Length); this.Invoke((EventHandler)(delegate { progressBar1.Maximum = Convert.ToInt32(fi.Length); tlblState.Text = "状态:开始发送文件内容..."; })); FileStream fs = fi.OpenRead(); try { byte[] buffer = new byte[8192]; int len; while ((len = fs.Read(buffer, 0, 8192)) != 0) { writer.Write(buffer, 0, len); this.Invoke((EventHandler)(delegate { progressBar1.Value = len; })); } this.Invoke((EventHandler)(delegate { progressBar1.Value = 0; tlblState.Text = "状态:文件内容发送完成!"; btnCancel.Enabled = false; btnStart.Enabled = true; })); } finally { fs.Close(); } } catch (Exception ex2) { this.Invoke((EventHandler)(delegate { progressBar1.Value = 0; tlblState.Text = "状态:文件发送过程出现错误!"; btnCancel.Enabled = false; btnStart.Enabled = true; })); } } catch (Exception ex3) { this.Invoke((EventHandler)(delegate { progressBar1.Value = 0; tlblState.Text = "状态:用户中断!"; btnCancel.Enabled = false; btnStart.Enabled = true; })); } finally { worker = null; } } private void Form1_Load(object sender, EventArgs e) { btnCancel.Enabled = false; btnStart.Enabled = true; } private void btnCancel_Click(object sender, EventArgs e) { worker.Abort(); btnCancel.Enabled = false; btnStart.Enabled = true; } }}

 接收端代码:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.IO;using System.Threading;namespace Receiver{ public partial class Form1 : Form { Thread worker; int port; public Form1() { InitializeComponent(); } private void btnStartListen_Click(object sender, EventArgs e) { port = int.Parse(txtPort.Text); btnStartListen.Enabled = false; btnCancel.Enabled = true; worker = new Thread(new ThreadStart(Start)); worker.IsBackground = true; worker.Start(); } private void Start() { try { this.Invoke((EventHandler)(delegate { tlblState.Text = "状态:开始监听中..."; })); TcpListener tcpListener = new TcpListener(IPAddress.Any, port); try { tcpListener.Start(); } catch (Exception ex1) { this.Invoke((EventHandler)(delegate { tlblState.Text = "状态:监听端口出错!"; btnCancel.Enabled = false; btnStartListen.Enabled = true; })); worker.Abort(); return; } try { this.Invoke((EventHandler)(delegate { progressBar1.Minimum = 0; tlblState.Text = "状态:开始接收文件信息!"; })); while (true) { //下面的循环作用是等待新的连接请求到来 while (!tcpListener.Pending()) Thread.Sleep(100); TcpClient tcpClient = tcpListener.AcceptTcpClient(); tcpClient.NoDelay = true; tcpClient.ReceiveTimeout = 30000; BinaryReader reader = new BinaryReader(tcpClient.GetStream()); try { string filename = reader.ReadString(); long total = reader.ReadInt64(); this.Invoke((EventHandler)(delegate { progressBar1.Maximum = Convert.ToInt32(total); tlblState.Text = "状态:你正接收到文件:" filename ",文件大小为:" total "字节."; }));string saveAs = null; SaveFileDialog sfd = new SaveFileDialog(); sfd.FileName = filename; this.Invoke((EventHandler)(delegate { if (sfd.ShowDialog(this) == DialogResult.OK) saveAs = sfd.FileName; else saveAs = null; })); if (saveAs == null) { this.Invoke((EventHandler)(delegate { tlblState.Text = "状态:你拒绝了该文件!"; })); } else { this.Invoke((EventHandler)(delegate { tlblState.Text = "状态:文件接收中..."; })); FileStream fs = File.Create(saveAs); try { byte[] buffer = new byte[8192]; int len; while (total > 0) { len = reader.Read(buffer, 0, 8192); if (len == 0) { this.Invoke((EventHandler)(delegate { tlblState.Text = "状态:对方终止连接!"; btnCancel.Enabled = false; btnStartListen.Enabled = true; worker.Abort(); return; })); } fs.Write(buffer, 0, len); this.Invoke((EventHandler)(delegate { progressBar1.Value = len; })); total -= len; } this.Invoke((EventHandler)(delegate { tlblState.Text = "状态:文件接收完成!"; progressBar1.Value = 0; })); } finally { fs.Close(); } } } catch (Exception ex2) { this.Invoke((EventHandler)(delegate { tlblState.Text = "状态:文件接收中途出错!"; btnCancel.Enabled = false; btnStartListen.Enabled = true; })); } finally { reader.Close(); tcpClient.Close(); } } } finally { tcpListener.Stop(); } } catch (Exception ex3) { this.Invoke((EventHandler)(delegate { tlblState.Text = "状态:用户中断!"; btnCancel.Enabled = false; btnStartListen.Enabled = true; })); } finally { worker = null; } } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复